// mircoApp/cultivateCourse/courseClass/courseClassDetail/courseClassDetail.js
const app = getApp();
const skinBehavior = require('../../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    course_term: [],
    searchVal: '',
    dialogShow: false,
    replaceShow: false,
    radio: '',
    nowTime: '',
    quota: [],
    quota_list: [],
    page: 1,
    total_page: 0,
    isLoadMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.nowTime = Date.now();
    this.setData({
      nowTime: this.data.nowTime
    })
    this.getData();
  },
  toAdress() {
    let point = app.BMapTransqqMap(this.data.course_term.lng, this.data.course_term.lat);
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
    })
  },
  onClickButton() {
    if(this.data.course_term.canEnroll){
      return
    }
    this.getQuota();
  },
  tapRadio(e) {
    let {
      id, item
    } = e.currentTarget.dataset;
    this.data.quota.quota = item;
    this.setData({
      quota: this.data.quota
    })
  },
  onChange(e) {
    console.log(e)
    this.setData({
      radio: e.detail
    })
  },
  onClose() {
    this.setData({
      replaceShow: false,
      dialogShow: true
    })
  },
  childMore() {
    console.log(this.data.quota_list)
    this.getQuota('more');

  },
  onClose_dialog() {
    this.setData({
      replaceShow: false,
      dialogShow: false,
      quota: [],
      quota_list: [],
    })
  },
  tapReplaceShow() {
    this.setData({
      replaceShow: true,
      dialogShow: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 报名
  cinfirmApply() {
    // this.onClose_dialog()
    let json = {
      id: this.options.id,
      goods_id: this.data.quota.quota.goods_id,
      course_type: this.data.quota.quota.course_type
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.term.detail.enroll"),
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg)
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none',
            success: () => {
              if (res.data.to_url) {
                setTimeout(() => {
                  wx.navigateTo({
                    url: res.data.to_url
                  })
                }, 1000);
              }
            }
          })

        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getQuota(key = '') {
    let json = {
      id: this.options.id,
      page: this.data.page
    }
    if (key == 'more') {
      if (this.data.total_page >= this.data.page) {
        return
      }
      this.setData({
        page: this.data.page + 1
      })
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.term.detail.get-quota-list"),
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          if (key == 'more') {
            this.setData({
              quota_list: this.data.quota_list.concat(res.data.quota_list)
            })
            return
          }
          this.data.isLoadMore = true;
          this.data.total_page = res.data.quota_list.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            dialogShow: true,
            quota: res.data,
            quota_list: res.data.quota_list,
            total_page: this.data.total_page
          })
        } else {
          if (key == 'more') {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false
            return
          }
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none',
            success: () => {
              setTimeout(() => {
                wx.navigateTo({
                  url: '/packageB/member/category/search_v2/search_v2?tag=cultivate_course&course_term_id='+this.options.id,
                })
              }, 1000);
            }
          })
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getData() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.cultivate-course.frontend.controllers.term.detail"),
      data: {
        id: this.options.id
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.data)
          this.setData({
            course_term: res.data.course_term
          })
          let endtime = Date.parse(this.data.course_term.end_time);
          console.log(this.data.nowTime, endtime)
          this.setData({
            endtime: endtime
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  }
})